<?php

/**
 * This is the model class for table "usuarios".
 *
 * The followings are the available columns in table 'usuarios':
 * @property string $id_usuarios
 * @property string $Empresa_id_empresa
 * @property string $Perfiles_id_Perfiles
 * @property string $nombre
 * @property string $puesto
 * @property string $correo_electronico
 * @property string $pass
 */
class Usuarios extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Usuarios the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Empresa_id_empresa, Perfiles_id_Perfiles', 'required'),
			array('Empresa_id_empresa, Perfiles_id_Perfiles', 'length', 'max'=>10),
			array('nombre, puesto, correo_electronico, pass', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_usuarios, Empresa_id_empresa, Perfiles_id_Perfiles, nombre, puesto, correo_electronico, pass', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'empresa'=>array(self::BELONGS_TO, 'Empresa', 'Empresa_id_empresa'),
        'telefonos'=>array(self::MANY_MANY, 'Telefonos', 'Telefonos_has_Usuarios(Telefonos_id_telefonos, Usuarios_id_usuarios)'),
		'perfiles'=>array(self::BELONGS_TO, 'Perfiles', 'Perfiles_id_Perfiles'),
		'ticket'=>array(self::MANY_MANY, 'Ticket', 'UsuariosHasTicket(Ticket_id_ticket, Usuarios_id_usuarios)'),
		'respuesta'=>array(self::HAS_MANY, 'Respuesta', 'Usuarios_id_usuarios'),
		);
	}

/**
 * @return boolean validate user
 */
public function validatePassword($pass, $correo_electronico){
    return $this->hashPassword($pass, $correo_electronico);
} 
/**
 * @return hashed value
 */
//DEFINE('SALT_LENGTH', 10);
public function hashPassword($phrase, $salt = null){
    $key = 'Gf;B&yXL|beJUf-K*PPiU{wf|@9K9j5?d+YW}?VAZOS%e2c -:11ii<}ZM?PO!96';
    if($salt == '')
        $salt = substr(hash('sha512', $key), 0, 10);
    else
        $salt = substr($salt, 0, 10);
    return hash('sha512', $salt . $key . $phrase);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_usuarios' => 'Id Usuarios',
			'Empresa_id_empresa' => 'Empresa Id Empresa',
			'Perfiles_id_Perfiles' => 'Perfiles Id Perfiles',
			'nombre' => 'Nombre',
			'puesto' => 'Puesto',
			'correo_electronico' => 'Correo Electronico',
			'pass' => 'Pass',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_usuarios',$this->id_usuarios,true);
		$criteria->compare('Empresa_id_empresa',$this->Empresa_id_empresa,true);
		$criteria->compare('Perfiles_id_Perfiles',$this->Perfiles_id_Perfiles,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('puesto',$this->puesto,true);
		$criteria->compare('correo_electronico',$this->correo_electronico,true);
		$criteria->compare('pass',$this->pass,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}